import axios from "axios";
import querystring from "querystring"

const errorHandle = (status, other) => {
    switch (status) {
        case 400:
            console.log("服务器出错");
            break;
        case 401:
            console.log("未授权访问");
            break;
        case 403:
            console.log("服务器拒绝访问");
            break;
        case 404:
            console.log("请求地址出错");
            break;
        case 408:
            console.log("请求超时");
            break;
        case 500:
            console.log("服务器内部错误");
            break;
        case 501:
            console.log("服务未实现");
            break;
        case 502:
            console.log("网关错误");
            break;
        case 503:
            console.log("服务不可用");
            break;
        case 504:
            console.log("网关超时");
            break;
        case 505:
            console.log("HTTP版本不受支持");
            break;
        default:
            console.log(other);
    }
}


const instance = axios.create({
    // baseURL: "http://localhost:8080/",
    timeout: 5000,
    headers: {
        "Content-Type": "application/json"
    }
});

//请求拦截器
instance.interceptors.request.use(
    config => {
        if (config.method === "post") {
            //其他拦截处理
        }
        return config;
    },

    error => {
        return Promise.reject(error);
    }
);

//响应拦截器
instance.interceptors.response.use(
    response => {
        console.log(response)
        if (response.status === 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        const {response} = error;
        errorHandle(response.status, response.info)
    }
);


export default instance;